<template>
    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body @close="close">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="角色名称" prop="roleName">
                <el-input v-model="form.roleName" placeholder="请输入角色名称"/>
            </el-form-item>
            <el-form-item label="权限字符" prop="roleKey">
                <el-input v-model="form.roleKey" placeholder="请输入权限字符"/>
            </el-form-item>
            <el-form-item label="状态">
                <el-radio-group v-model="form.status">
                    <el-radio
                            v-for="dict in statusOptions"
                            :key="dict.dictValue"
                            :label="dict.dictValue"
                    >{{ dict.dictLabel }}
                    </el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="角色描述">
                <el-input v-model="form.roleDesc" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import {updateRole, addRole} from '/src/api/system/role'

    export default {
        name: "update",
        emits: ['dialogStatusChanged'],
        props: {
            open: Boolean,
            title: String,
            statusOptions: Array,
            form: Object,
        },
        data() {
            return {
                // 表单校验
                rules: {
                    roleName: [
                        {required: true, message: "角色名称不能为空", trigger: "blur"}
                    ],
                    roleKey: [
                        {required: true, message: "权限字符不能为空", trigger: "blur"}
                    ]
                }
            }
        },
        methods: {
            //提交
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        if (this.form.roleId !== undefined) {
                            updateRole(this.form).then(response => {
                                this.msgSuccess('修改成功')
                                this.close()
                            })
                        } else {
                            addRole(this.form).then(response => {
                                this.msgSuccess('新增成功')
                                this.close()
                            })
                        }
                    }
                })
            },
            //取消
            cancel() {
                this.$emit('dialogStatusChanged', false)
            },
            //关闭
            close() {
                this.$emit('dialogStatusChanged', false)
            }
        }
    }
</script>

<style scoped>

</style>
